﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Collections;

namespace Acsys.Entity
{
    public class Owner : IEntity
    {
        public int ID = 0;
        public string CODE = "D00000";
        public string NAME = "";
        public string ADDRESS = "";
        public string PHONE = "";

        public Owner()
        {
        }
        public Owner(int id)
        {
            ID = id;
        }
        public Owner(int id, string code)
        {
            ID = id;
            CODE = code;
        }
        public IEntity Get(MySqlDataReader aReader)
        {
            Owner owner = null;
            while (aReader.Read())
            {
                owner = new Owner();
                owner.ID = Convert.ToInt32(aReader[0]);
                owner.CODE = aReader[1].ToString();
                owner.NAME = aReader[2].ToString();
                owner.ADDRESS = aReader[3].ToString();
                owner.PHONE = aReader[4].ToString();
            }
            aReader.Close();
            return owner;
        }
        public string GetInsertSQL()
        {
            return String.Format(@"insert into table_owner 
                (owner_code,owner_name,owner_address,owner_phone) 
                VALUES ('{0}','{1}','{2}','{3}')",
                CODE, NAME, ADDRESS, PHONE);
        }
        public string GetDeleteSQL()
        {
            return "delete from table_owner where owner_id = " + ID;
        }
        public string GetUpdateSQL()
        {
            return String.Format(@"update table_owner set 
                owner_code = '{0}', 
                owner_name='{1}',
                owner_address='{2}',
                owner_phone='{3}'
                where owner_id = {4}",
                CODE, NAME,ADDRESS, PHONE, ID);
        }
        public string GetByIDSQL(int ID)
        {
            return String.Format("select * from table_owner where owner_id = {0}", ID);
        }
        public string GetByCodeSQL(string code)
        {
            return String.Format("select * from table_owner where owner_code = '{0}'", code);
        }
        public string GetByCodeLikeSQL(string text)
        {
            return String.Format("select * from table_owner where owner_code like '%{0}%'", text);
        }
        public string GetByNameLikeSQL(string text)
        {
            return String.Format("select * from table_owner where owner_name like '%{0}%'", text);
        }
        public string GetAllSQL()
        {
            return String.Format("select * from table_owner");
        }
        public IList GetAll(MySqlDataReader aReader)
        {
            IList result = new ArrayList();
            while (aReader.Read())
            {
                Owner owner = new Owner();
                owner.ID = Convert.ToInt32(aReader[0]);
                owner.CODE = aReader[1].ToString();
                owner.NAME = aReader[2].ToString();
                owner.ADDRESS = aReader[3].ToString();
                owner.PHONE = aReader[4].ToString();
                result.Add(owner);
            }
            aReader.Close();
            return result;
        }
        public int GetID()
        {
            return ID;
        }
        public void SetID(int id)
        {
            ID = id;
        }
        public override string ToString()
        {
            return CODE +" "+NAME;
        }
        public string GetCode()
        {
            return CODE;
        }
        public void SetCode(string code)
        {
            CODE = code;
        }
        public string GetMaximumIDSQL()
        {
            return String.Format("SELECT max(owner_id) from table_owner");
        }
    }
}
